import Vue from 'vue'
import App from './App'
import router from './router'
import Axios from './config/base.js'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(Element);
Vue.config.productionTip = false;
Vue.prototype.router = router;
Vue.prototype.$axios = Axios;
router.beforeEach((to, from, next) => {
  //to即将进入的目标路由对象，from当前导航正要离开的路由， next : 下一步执行的函数钩子
  if (to.path === '/login') {
    next()
  } // 如果即将进入登录路由，则直接放行
  else {   //进入的不是登录路由

    if (to.meta.requiresAuth && (sessionStorage.getItem('user') == null || sessionStorage.getItem('user') == '')) {
      next({path: '/login'})
    }
    //下一跳路由需要登录验证，并且还未登录，则路由定向到 登录路由
    else {
      if(window.location.href.indexOf('?')!=-1 && sessionStorage.getItem("defaultId")==null) {
        sessionStorage.setItem("defaultId",window.location.href.split('?')[1]);
        location.replace(location.href.replace(location.search, ""));
        next({path: '/main'})
      }else{
        next()
      }
    }
  }
//如果不需要登录验证，或者已经登录成功，则直接放行
});
new Vue({
  el: '#app',
  router,
  components: {App},
  template: '<App/>'
});
